package org.wei.rssreader;

import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.sun.syndication.feed.synd.SyndFeed;


public class RssReaderServlet extends HttpServlet {

	/* (non-Javadoc)
	 * @see javax.servlet.http.HttpServlet#doGet(javax.servlet.http.HttpServletRequest, javax.servlet.http.HttpServletResponse)
	 */
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		String path="rssView.jsp";
		List<RssSourceBO> list = RssDAO.getAll();
		List<SyndFeed> feeds = new ArrayList<SyndFeed>();
		if(list!=null) {
			for(RssSourceBO bo: list) {
				SyndFeed feed = RssUtil.fatchByUrl(new URL(bo.getUrl()));
				if(feed!=null) {feeds.add(feed);} //
			}
		}
		req.setCharacterEncoding("UTF-8");
		req.setAttribute("feeds", feeds);

		try {
			req.getRequestDispatcher(path).forward(req, resp);
		} catch (ServletException e) {
			e.printStackTrace();
		}
	}

}
